﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public enum EMoveOri{
    E_MID,
    E_LEFT,
    E_RIGHT
}
public interface ICameraFollowTarget{
    Vector2 GetPosition();
    EMoveOri GetMoveOri();
}

public interface ICameraCommonHandle {
    void CameraFollowTarget(ICameraFollowTarget target);
    void CameraFollowTargetHoriPrefer(ICameraFollowTarget target, float preferv);
    void Shake(float durtion, float volume, int prio);
}

public class CameraMgr : MonoBehaviour,ICameraCommonHandle
{
    public float LerpValue = 0.04f;
    public float OriPrefenceDiss = 1.5f;
    public ICameraFollowTarget target_ = null;
    

    public Transform child1Camera_ = null;
    public ActionProcesser child2Camera_ = null;
    // Update is called once per frame
    void Update()
    {
        if (this.target_ != null) {
            var targetPos = this.target_.GetPosition();
            this.transform.position = new Vector3(targetPos.x, targetPos.y, -6);

            var diss = 0.0f;
            var ori = this.target_.GetMoveOri();
            if (ori == EMoveOri.E_LEFT)
            {
                diss = OriPrefenceDiss * -1.0f;
            }
            else if (ori == EMoveOri.E_RIGHT)
            {
                diss = OriPrefenceDiss * 1.0f;
            }
            var oriPos = this.child1Camera_.localPosition;
            this.child1Camera_.localPosition = Vector3.Lerp(oriPos, new Vector3(diss, this.child1Camera_.localPosition.y, this.child1Camera_.localPosition.z), LerpValue);

        }

        // var targetPos = this.target_.GetPosition();
        // var diss = 0.0f;
        // var ori = this.target_.GetMoveOri();
        // if (ori == EMoveOri.E_LEFT) {
        //     diss = OriPrefenceDiss * -1.0f;
        // } else if (ori == EMoveOri.E_RIGHT) {
        //     diss = OriPrefenceDiss * 1.0f;
        // 
        // }
        // var posNow = this.transform.position;
        // 
        // var aimPos = new Vector2(targetPos.x + diss, targetPos.y) ;
        // aimPos = Vector2.Lerp(posNow,aimPos,0.05f);
        // this.transform.position = new Vector3( aimPos.x, aimPos.y,-6) ;

        // if (Input.GetKeyDown(KeyCode.Q)) {
        //     PrefabsMgr.GetInstance().Send(
        //         PrefabsMgr.TYPE_IDIOT,
        //         new WakeUpConfigIdiot(
        //             new Vector2(0,0),400
        //             ));
        // }

        // if (Input.GetKeyDown(KeyCode.L)) {
        //     if (this.child2Camera_ != null) {
        //         if (this.child2Camera_.ActionsCount()<=0) {
        //             this.child2Camera_.RunAction(new ActionShake(this.child2Camera_.transform,0.03f,0.3f));
        //         }
        //     }
        //     var pos = target_.GetPosition();
        //     float adiss = 0.3f;
        //     var xrd = UnityEngine.Random.Range(0, adiss * 2) - adiss * 2;
        //     var yrd = UnityEngine.Random.Range(0, adiss * 2) - adiss * 2;
        //     if (ori == EMoveOri.E_LEFT)
        //     {
        //         PrefabsMgr.GetInstance().Send(
        //        PrefabsMgr.TYPE_BULLET_CACHE,
        //        new WakeUpConfigBulletShell(new Vector2(pos.x, pos.y + 0.5f), new Vector2(1 + xrd, 1 + yrd), -1000));
        //     }
        //     else if (ori == EMoveOri.E_RIGHT)
        //     {
        //         PrefabsMgr.GetInstance().Send(
        //            PrefabsMgr.TYPE_BULLET_CACHE,
        //            new WakeUpConfigBulletShell(new Vector2(pos.x, pos.y + 0.5f), new Vector2(-1 + xrd, 1 + yrd), 1000));
        //     }
        // }


    }

    public static CameraMgr instance_;
    public static CameraMgr GetInstance() {
        return instance_;
    }
    private void Awake()
    {
        instance_ = this;
    }

    private void Start()
    {

       

        this.child1Camera_ = this.transform.GetChild(0).transform;
        this.child2Camera_ = this.transform.GetChild(0).transform.GetChild(0).GetComponent<ActionProcesser>();
    }

    public void CameraFollowTarget(ICameraFollowTarget target)
    {
        this.CameraFollowTargetHoriPrefer(target, 0);
    }

    public void CameraFollowTargetHoriPrefer(ICameraFollowTarget target, float preferv)
    {
        this.target_ = target;
        this.OriPrefenceDiss = preferv;
    }

    private int shakePriotity_ = 0;
    public void Shake(float durtion, float volume, int prio)
    {
        if (this.child2Camera_ != null)
        {
            if (this.child2Camera_.ActionsCount() <= 0)
            {
                this.shakePriotity_ = prio;
                this.child2Camera_.RunAction(new ActionShake(this.child2Camera_.transform, volume, durtion));
            }
            else {
                if (prio >= this.shakePriotity_) {
                    this.child2Camera_.StopAllActions();
                    this.child2Camera_.RunAction(new ActionShake(this.child2Camera_.transform, volume, durtion));
                    this.shakePriotity_ = prio;
                }
            }
        }
    }
}
